package de.breitbeil.jbooking.ui.preferences;

import java.io.IOException;

import org.springframework.richclient.form.Form;
import org.springframework.richclient.preference.FormBackedPreferencePage;

import de.breitbeil.jbooking.ui.form.FtpCalendarForm;
import de.breitbeil.jbooking.ui.form.FtpCalendarFormObject;

public class FtpPreferencePage extends FormBackedPreferencePage {

    public static final String ID = "ftpPreferencePage";
    
    private FtpCalendarFormObject formObject;
    
    public FtpPreferencePage() {
        super(ID, true);
        
        formObject = new FtpCalendarFormObject();
        
        setTitle(getMessage(ID + ".title"));
        setDescription(getMessage(ID + ".description"));
    }
    
    @Override
    protected Form createForm() {
        formObject.init(getSettings());
        
        FtpCalendarForm form = new FtpCalendarForm(formObject);
        form.getFormModel().setValidating(false);
        
        return form;
    }
    
    @Override
    protected void onDefaults() {
        super.onDefaults();
        formObject.fillDefaults(getForm().getFormModel());
    }
    
    @Override
    protected boolean onFinish() {
        getForm().commit();
        SettingsFormObject formObject = (SettingsFormObject)getForm().getFormObject();
        formObject.save(getSettings());
        
        try {
            getSettings().save();
            return true;
        } catch (IOException e) {
            throw new RuntimeException("Can't save settings", e);
        }
    }
    
}
